# 自定义回调
from langchain.callbacks.base import BaseCallbackHandler
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from openaiConfigurations import openai_api_key, openai_api_base
class MyCustomHandler(BaseCallbackHandler):
    def on_llm_new_token(self, token: str, **kwargs) -> None:
        print(f"自定义回调处理器，token: {token}")

# 必须开启流式传输
chat = ChatOpenAI(max_tokens = 25, 
                  streaming = True, 
                  openai_api_key = openai_api_key,
                  openai_api_base = openai_api_base,
                  callbacks = [MyCustomHandler()])

print(chat([HumanMessage(content = "中国的首都是哪里？")]))